package com.nl.pulsardemo.pool.controller;

import com.nl.pulsardemo.pool.config.consumer.PooledConsumerFactory;
import com.nl.pulsardemo.pool.config.consumer.MyConsumerPoolConfig;
import com.nl.pulsardemo.pool.service.IConsumer;
import com.nl.pulsardemo.pool.vo.ResultVO;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.pulsar.client.api.Consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author : ZhaoY
 * @description :
 * @date : 2021-02-01 16:11
 **/
@Controller
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    private IConsumer iConsumer;
    @Autowired
    private GenericObjectPool<Consumer> consumerPool;
//    GenericObjectPool<Consumer> consumerPool=new GenericObjectPool(new PooledConsumerFactory(), new MyConsumerPoolConfig());


    @GetMapping("async")
    @ResponseBody
    @CrossOrigin(origins = "*")
    public ResultVO asyncReceive() throws Exception {
        Consumer consumer = consumerPool.borrowObject();
        ResultVO resultVO = iConsumer.AsyncReceive(consumer);
        consumerPool.returnObject(consumer);
        return resultVO;
    }

    @GetMapping("/sync")
    @ResponseBody
    @CrossOrigin(origins = "*")
    public ResultVO syncReceive() throws Exception {
        Consumer consumer = consumerPool.borrowObject();
        ResultVO resultVO = iConsumer.SyncReceive(consumer);
        consumerPool.returnObject(consumer);
        return resultVO;
    }
}
